package com.softserve.academy.food.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import com.softserve.academy.food.model.mUserCredentials;
import com.softserve.academy.food.model.mUserInfo;
import com.softserve.academy.food.security.Coding;

@Entity
@Table( name = "USERS" )
public class eUser 
{
	@Id
	@Column(name="USER_ID")
	@GeneratedValue(generator="increment")
	@GenericGenerator(name="increment", strategy = "increment")
	private int id;
	@Column(name="USERNAME")
	private String login;
	@Column(name="PASSWORD")
	private String pass;
	@Column(name="ENABLED")
	private boolean enable=true;
	@Column(name="AUTHORITY")
	private String role="ROLE_USER";
	
	private String fistname;
	private String lastname;
	private String mail;
	private String phone;
	private String adres;
	
	public eUser()
	{	
	}
	
	public eUser( String login, String pass )
	{	
		this.login = login;
		
		Coding coding = new Coding();
		this.pass = coding.getHash(pass);
	}
	
	public eUser( String login, String pass, String role )
	{	
		this.login = login;
		Coding coding = new Coding();
		this.pass = coding.getHash(pass);
		this.role = role;
	}
	
	public eUser( mUserCredentials user )
	{	
		id = user.getId();
		login = user.getLogin();
		pass = user.getPass();
		role = user.getRole();
	}
	
	public eUser(mUserInfo user)
	{	
		id = user.getId();
		fistname = user.getFistname();
		lastname = user.getLastname();
		mail = user.getMail();
		phone = user.getPhone();
		adres = user.getAdres();
	}

	public int getId() 
	{
		return id;
	}

	public void setId( int id ) 
	{
		this.id = id;
	}

	public String getLogin() 
	{
		return login;
	}

	public void setLogin( String login ) 
	{
		this.login = login;
	}

	public String getPass() 
	{
		return pass;
	}

	public void setPass( String pass ) 
	{
		this.pass = pass;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable( boolean enable ) 
	{
		this.enable = enable;
	}

	public String getRole() 
	{
		return role;
	}

	public void setRole( String role ) 
	{
		this.role = role;
	}

	public String getFistname() 
	{
		return fistname;
	}

	public void setFistname( String fistname ) 
	{
		this.fistname = fistname;
	}

	public String getLastname() 
	{
		return lastname;
	}

	public void setLastname( String lastname ) 
	{
		this.lastname = lastname;
	}

	public String getMail() 
	{
		return mail;
	}

	public void setMail( String mail ) 
	{
		this.mail = mail;
	}

	public String getPhone() 
	{
		return phone;
	}

	public void setPhone( String phone ) 
	{
		this.phone = phone;
	}

	public String getAdres() 
	{
		return adres;
	}

	public void setAdres( String adres ) 
	{
		this.adres = adres;
	}

	@Override
	public int hashCode() 
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((adres == null) ? 0 : adres.hashCode());
		result = prime * result + (enable ? 1231 : 1237);
		result = prime * result
				+ ((fistname == null) ? 0 : fistname.hashCode());
		result = prime * result + id;
		result = prime * result
				+ ((lastname == null) ? 0 : lastname.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((mail == null) ? 0 : mail.hashCode());
		result = prime * result + ((pass == null) ? 0 : pass.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		result = prime * result + ((role == null) ? 0 : role.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof eUser))
			return false;
		eUser other = (eUser) obj;
		if (adres == null) 
		{
			if (other.adres != null)
				return false;
		} 
		else if (!adres.equals(other.adres))
			return false;
		if (enable != other.enable)
			return false;
		if (fistname == null) 
		{
			if (other.fistname != null)
				return false;
		} else if (!fistname.equals(other.fistname))
			return false;
		if (id != other.id)
			return false;
		if (lastname == null) 
		{
			if (other.lastname != null)
				return false;
		} 
		else if (!lastname.equals(other.lastname))
			return false;
		if (login == null) 
		{
			if (other.login != null)
				return false;
		} 
		else if (!login.equals(other.login))
			return false;
		if (mail == null) 
		{
			if (other.mail != null)
				return false;
		} else if (!mail.equals(other.mail))
			return false;
		if (pass == null) 
		{
			if (other.pass != null)
				return false;
		} 
		else if (!pass.equals(other.pass))
			return false;
		if (phone == null) 
		{
			if (other.phone != null)
				return false;
		} 
		else if (!phone.equals(other.phone))
			return false;
		if (role == null) 
		{
			if (other.role != null)
				return false;
		} 
		else if (!role.equals(other.role))
			return false;
		return true;
	}

	@Override
	public String toString() 
	{
		return "eUser [id=" + id + ", login=" + login + ", pass=" + pass
				+ ", enable=" + enable + ", role=" + role + ", fistname="
				+ fistname + ", lastname=" + lastname + ", mail=" + mail
				+ ", phone=" + phone + ", adres=" + adres + "]";
	}




	
}
